package com.lmt.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * Zuul过滤器
 * Date 2018/06/30
 *
 * @author MT-Lin
 */
public class PreRequestLogFilter extends ZuulFilter {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 返回过滤器类型
     * 类型：pre、route、post、error等
     * @return 过滤器类型
     */
    @Override
    public String filterType() {
        return "pre";
    }

    /**
     * 指定过滤器的执行顺序，不同的过滤器允许返回相同的数字
     * @return 1
     */
    @Override
    public int filterOrder() {
        return 1;
    }

    /**
     * 判断该过滤器是否要执行
     * @return true表示执行，false表示不执行
     */
    @Override
    public boolean shouldFilter() {
        return true;
    }

    /**
     * 过滤器的具体逻辑
     * @return null
     * @throws ZuulException Zuul异常
     */
    @SuppressWarnings("RedundantThrows")
    @Override
    public Object run() throws ZuulException {
        RequestContext requestContext = RequestContext.getCurrentContext();
        HttpServletRequest request = requestContext.getRequest();
        logger.warn("send {} request to {}", request.getMethod(), request.getRequestURL());
        return null;
    }
}
